﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Win32;

namespace Smartkernel
{
    /// <summary>
    /// 智能进程：提供进程相关的操作
    /// </summary>
    public static class SmartProcess
    {
        /// <summary>
        /// 启动程序，注意，默认的情况下，窗口正常打开，无参数
        /// </summary>
        /// <param name="processPath">待启动的程序路径</param>
        public static void Start(string processPath)
        {
            Start(processPath, ProcessWindowStyle.Normal, string.Empty);
        }
        /// <summary>
        /// 启动程序
        /// </summary>
        /// <param name="processPath">待启动的程序路径</param>
        /// <param name="processWindowStyle">窗口样式</param>
        /// <param name="args">参数</param>
        public static void Start(string processPath, ProcessWindowStyle processWindowStyle, string args)
        {
            using (Process process = new Process())
            {
                process.StartInfo.FileName = processPath;
                process.StartInfo.Arguments = args;
                process.StartInfo.WindowStyle = processWindowStyle;
                process.Start();
            }
        }
        /// <summary>
        /// 执行命令行命令
        /// </summary>
        /// <param name="commandLine">命令，例如：ping 211.66.4.102</param>
        /// <returns>返回执行的结果</returns>
        public static string ExecuteCommandLine(string commandLine)
        {
            string output = string.Empty;
            using (Process process = new Process())
            {
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                process.StandardInput.WriteLine(commandLine);
                process.StandardInput.WriteLine("exit");
                output = process.StandardOutput.ReadToEnd();
            }
            return output;
        }
        /// <summary>
        /// 系统进程列表
        /// </summary>
        public static List<Process> AllProcesses
        {
            get
            {
                return new List<Process>(Process.GetProcesses());
            }
        }
        /// <summary>
        /// 关闭进程
        /// </summary>
        /// <param name="processName">进程名称</param>
        public static void Stop(string processName)
        {
            Process[] processes = Process.GetProcessesByName(processName);
            foreach (Process process in processes)
            {
                process.EnableRaisingEvents = false;
                if (process.HasExited == false)
                {
                    //先尝试关闭主窗口，如果没有主窗口，就关闭主进程（关闭主窗口时会自动关闭主进程，反之不然，但是没有窗口的程序也只能通过关闭主进程关闭了）
                    if (process.CloseMainWindow() == false)
                    {
                        process.Kill();
                    }
                }
            }
        }
        /// <summary>
        /// 设置自动运行
        /// </summary>
        /// <param name="processPath">程序路径</param>
        /// <param name="shortName">简称</param>
        public static void SetAutoRun(string processPath, string shortName)
        {
            string autoRunPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
            RegistryKey registryKey = null;
            try
            {
                registryKey = Registry.LocalMachine.OpenSubKey(autoRunPath, true);
                if (registryKey == null)
                {
                    registryKey = Registry.LocalMachine.CreateSubKey(autoRunPath);
                }
                registryKey.SetValue(shortName, processPath);
            }
            finally
            {
                registryKey.Close();
            }
        }
        /// <summary>
        /// 删除自动运行
        /// </summary>
        /// <param name="shortName">简称</param>
        public static void DeleteAutoRun(string shortName)
        {
            string autoRunPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
            using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(autoRunPath, true))
            {
                if (registryKey != null)
                {
                    registryKey.DeleteValue(shortName);
                }
            }
        }
        /// <summary>
        /// 调用批处理程序文件
        /// </summary>
        /// <param name="batchFilePath">批处理文件路径</param>
        /// <returns>执行的结果</returns>
        public static string CallBatchFile(string batchFilePath)
        {
            return ExecuteCommandLine("call " + batchFilePath);
        }
        /// <summary>
        /// 复制源文件夹中的所有文件到目标文件夹中，注意，只复制一层目录，不会递归到下一层。
        /// </summary>
        /// <param name="sourceFolderPath">源文件夹</param>
        /// <param name="targetFolderPath">目标文件夹</param>
        /// <returns>执行的结果</returns>
        public static string CopyFiles(string sourceFolderPath, string targetFolderPath)
        {
            return ExecuteCommandLine("copy " + sourceFolderPath + " " + targetFolderPath);
        }
    }
}